package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.common.R;
import org.example.entity.DTO.LandmarkDTO;
import org.example.entity.Landmarks;
import org.example.entity.VisitorRecords;
import org.example.service.LandmarksService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO 类描述
 *
 * @author shaoxuan.
 * @date 2024/1/18 20:02
 */
@RestController
@RequestMapping("/landmark")
@Slf4j
@Api(tags = "地标管理")
public class LandmarkController {
   @Autowired
    private LandmarksService service;
    @PostMapping("/addLandmark")
    @ApiOperation("新增地标")
    public R<LandmarkDTO> addRecord(@RequestBody Landmarks landmarks){
        boolean save = service.save(landmarks);
        if (save){
            LandmarkDTO dto = new LandmarkDTO();
            BeanUtils.copyProperties(landmarks,dto);
            LandmarkDTO.Label label = new LandmarkDTO.Label();
            BeanUtils.copyProperties(landmarks,label);
            dto.setLabel(label);
            return R.success(dto).setMsg("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID查询")
    public R<LandmarkDTO> getById(@PathVariable("id") String id){
        Landmarks landmarks = service.getById(id);
        LandmarkDTO dto = new LandmarkDTO();
        BeanUtils.copyProperties(landmarks,dto);
        LandmarkDTO.Label label = new LandmarkDTO.Label();
        BeanUtils.copyProperties(landmarks,label);
        dto.setLabel(label);
        return R.success(dto);
    }

    @PutMapping("/update")
    @ApiOperation("修改地标")
    public R<LandmarkDTO> getById(Landmarks landmarks){
        boolean res = service.updateById(landmarks);
        if(res){
            LandmarkDTO dto = new LandmarkDTO();
            BeanUtils.copyProperties(landmarks,dto);
            LandmarkDTO.Label label = new LandmarkDTO.Label();
            BeanUtils.copyProperties(landmarks,label);
            dto.setLabel(label);
            return R.success(dto).setMsg("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除地标")
    public R<LandmarkDTO> delete(@PathVariable("id") String id){
        boolean res = service.removeById(id);
        if(res){
            return R.success(null).setMsg("删除成功");
        }else {
            return R.error("删除失败");
        }
    }


    @GetMapping("/getAll")
    @ApiOperation("查询所有地标")
    public R<List<LandmarkDTO>> getAll(){
        List<Landmarks> list = service.list();
        List<LandmarkDTO> res = new ArrayList<>();
        list.forEach(i->{
            LandmarkDTO dto = new LandmarkDTO();
            BeanUtils.copyProperties(i,dto);
            LandmarkDTO.Label label = new LandmarkDTO.Label();
            BeanUtils.copyProperties(i,label);
            dto.setLabel(label);
            res.add(dto);
        });
        return R.success(res);
    }


}
